package com.sky.service.impl;

import com.sky.entity.Orders;
import com.sky.mapper.DishMapper;
import com.sky.mapper.OrderMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.WorkspaceService;
import com.sky.vo.BusinessDataVO;
import com.sky.vo.DishOverViewVO;
import com.sky.vo.OrderOverViewVO;
import com.sky.vo.SetmealOverViewVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@Service
public class WorkspaceServiceImpl implements WorkspaceService {
    @Autowired
    private OrderMapper orderMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DishMapper dishMapper;
    @Autowired
    private SetmealMapper setmealMapper;
    /**
     * 今日数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public BusinessDataVO getBusinessData(LocalDateTime begin, LocalDateTime end) {
       //今日数据业务包括填充营业额，有效订单，订单完成率,平均客单价，新增用户
       //营业额数据
        Map mp = new HashMap();
        mp.put("begin",begin);
        mp.put("end",end);
        //得到全部订单数
        Integer orderCount = orderMapper.countByMap(mp);
        //得到有效订单数
        mp.put("status",Orders.COMPLETED);
        Integer validOrderCount = orderMapper.countByMap(mp);
        //得到营业额
        Double turnover = orderMapper.sumByMap(mp);
        turnover = turnover == null ? 0 : turnover;
        //得到新增用户数
        Integer newUser = userMapper.countByMap(mp);
        Double avgPrice = 0.0;
        Double orderRate = 0.0;
        //得到平均客单价以及完单率
        //判断有效订单数是否为0，以及总订单数是否为0
        if(validOrderCount!=0&&orderCount!=0){
            avgPrice = turnover/validOrderCount;
            orderRate = (double)validOrderCount/orderCount;
        }
        return new BusinessDataVO(turnover,validOrderCount,orderRate,avgPrice,newUser);
    }

    /**
     * 订单概况
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderOverViewVO getOrderOverView(LocalDateTime begin, LocalDateTime end) {
        //订单概况业务包括待接单，待派送，已完成，已取消，全部订单等信息的填充
        Map mp = new HashMap();
        mp.put("begin",begin);
        mp.put("end",end);
        Integer totalOrder = orderMapper.countByMap(mp);
        mp.put("status",Orders.TO_BE_CONFIRMED);
        Integer toBeConfirmed = orderMapper.countByMap(mp);
        mp.put("status",Orders.CONFIRMED);
        Integer confirmed = orderMapper.countByMap(mp);
        mp.put("status",Orders.COMPLETED);
        Integer completed = orderMapper.countByMap(mp);
        mp.put("status",Orders.CANCELLED);
        Integer cancelled = orderMapper.countByMap(mp);
        return OrderOverViewVO.builder()
                .allOrders(totalOrder)
                .waitingOrders(toBeConfirmed)
                .deliveredOrders(confirmed)
                .completedOrders(completed)
                .cancelledOrders(cancelled)
                .build();
    }

    /**
     * 菜品总览
     * @return
     */
    @Override
    public DishOverViewVO getDishOverView() {
        //菜品总览业务包括已起售，已停售填充
        Integer onSale = dishMapper.countByStatus(1);
        Integer offSale = dishMapper.countByStatus(0);
        return DishOverViewVO.builder()
                .sold(onSale)
                .discontinued(offSale)
                .build();
    }

    /**
     * 套餐总览
     * @return
     */
    @Override
    public SetmealOverViewVO getSetmealOverView() {
        //套餐总览业务包括已起售，已停售填充
        Integer onSale = setmealMapper.countByStatus(1);
        Integer offSale = setmealMapper.countByStatus(0);
        return SetmealOverViewVO.builder()
                .sold(onSale)
                .discontinued(offSale)
                .build();
    }
}
